package com.sanda.androidcourse.chapter06;

import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.sanda.androidcourse.R;

public class FileStorageActivity extends AppCompatActivity {

    Button btnInnerFileWrite, btnInnerFileOpen;

    Button btnOuterFileWrite, btnOuterFileOpen;
    TextView txtFileContent;

    Button btnSpWrite, btnSpRead, btnSpDelete;

    Button btnSqlInsert, btnSqlUpdate, btnSqlDelete, btnSqlQuery;
    DataBaseHelper dbHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.chapter06_activity_file_storage);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        btnInnerFileWrite = findViewById(R.id.btn_inner_file_write);
        btnInnerFileOpen = findViewById(R.id.btn_inner_file_open);
        txtFileContent = findViewById(R.id.txt_file_content);

        btnOuterFileWrite = findViewById(R.id.btn_outer_file_write);
        btnOuterFileOpen = findViewById(R.id.btn_outer_file_open);


        btnSpWrite = findViewById(R.id.btn_sp_write);
        btnSpRead = findViewById(R.id.btn_sp_read);
        btnSpDelete = findViewById(R.id.btn_sp_delete);


        btnSqlQuery = findViewById(R.id.btn_sql_query);
        btnSqlDelete = findViewById(R.id.btn_sql_delete);
        btnSqlUpdate = findViewById(R.id.btn_sql_update);
        btnSqlInsert = findViewById(R.id.btn_sql_insert);

        btnInnerFileWrite.setOnClickListener(v -> {
            FileHelper.writeFileToInternalStorage(this, "test.txt", "Hello World!");
        });

        btnInnerFileOpen.setOnClickListener(v -> {
            String content = FileHelper.readFileFromInternalStorage(this, "test.txt");
            txtFileContent.setText(content);
        });


        btnOuterFileWrite.setOnClickListener(v -> {
            FileHelper.writeFileToExternalStorage(this, "test.txt", "Hello World!");
        });

        btnOuterFileOpen.setOnClickListener(v -> {
            String content = FileHelper.readFileFromExternalStorage(this, "test.txt");
            txtFileContent.setText(content);
        });

        btnSpWrite.setOnClickListener(v -> {
            SharedPreferencesHelper.writeSharedPreferences(this, "name", "Bob");
            Toast.makeText(this, "写数据成功", Toast.LENGTH_LONG).show();
        });

        btnSpRead.setOnClickListener(v -> {
            String content = SharedPreferencesHelper.readSharedPreferences(this, "name");
            txtFileContent.setText(content);
        });

        btnSpDelete.setOnClickListener(v -> {
            SharedPreferencesHelper.deleteSharedPreferences(this, "name");
        });


        dbHelper = new DataBaseHelper(this);

        btnSqlInsert.setOnClickListener(v -> {
            dbHelper.insert("Alice", 25);
            Toast.makeText(this, "插入成功", Toast.LENGTH_LONG).show();
        });

        btnSqlUpdate.setOnClickListener(v -> {
            dbHelper.update(1, "Bob", 30);
            Toast.makeText(this, "更新成功", Toast.LENGTH_LONG).show();
        });

        btnSqlDelete.setOnClickListener(v -> {
            dbHelper.delete(2);
            Toast.makeText(this, "删除成功", Toast.LENGTH_LONG).show();
        });

        btnSqlQuery.setOnClickListener(v -> {
            Cursor cursor = dbHelper.getAll();
            StringBuilder stringBuilder = new StringBuilder();
            try {
                while (cursor.moveToNext()) {
                    int id = cursor.getInt(cursor.getColumnIndexOrThrow("id"));
                    String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                    int age = cursor.getInt(cursor.getColumnIndexOrThrow("age"));
                    stringBuilder.append("Id: ").append(id).append(", Name: ").append(name).append(", Age: ").append(age).append("\n");
                }
                txtFileContent.setText(stringBuilder.toString());
                cursor.close();
            }catch (Exception e){
                e.printStackTrace();
            }

        });

    }
}